package com.swf.common.i18n;

import java.util.*;

import org.dom4j.*;
import org.slf4j.*;

import com.swf.common.exception.*;
import com.swf.common.utils.*;
import com.swf.common.xml.*;

public abstract class AbstractXMLReader {
	/**
	 * 相对路径
	 */
	protected String filePath;
	/**
	 * 读取文件的编码
	 */
	protected String encoding;
	/**
	 *文档根节点 
	 */
	protected Element rootNode;
	private static Logger logger = LoggerFactory
			.getLogger(AbstractXMLReader.class);
	
	public AbstractXMLReader() {
	}
	
	public AbstractXMLReader(String filePath) throws FwsException {
		this(filePath,null);
	}
	public AbstractXMLReader(String filePath,String encoding) throws FwsException {
		if (encoding!=null && encoding.length()!=0){
			this.encoding =  encoding;
		} else{
			this.encoding = LanguageUtils.getEncodingByType("xml");
		}
		String resourceContent = FwsFileUtils.loadFile(filePath, this.encoding);
		if (FwsStringUtils.isBlank(resourceContent)){
			throw new FwsException("资源文件读取异常");
		}
		try {
			Document document =Dom4jUtils.parseTextIngoreDtd(resourceContent);
			rootNode = document.getRootElement();
		} catch (DocumentException e) {
			logger.error("资源文件读取异常", e);
		}
	}
	
	/**
	 * 读取资源文件，将读出的资源按照(ID，资源对象)的方式生成HashMap
	 * @return
	 */
	 protected abstract Map getResources();
	 
	 
}
